package com.estore.model;
<<<<<<< HEAD

import java.util.HashSet;
import java.util.Objects;
import java.util.Set;
=======
import java.util.Objects;
>>>>>>> a021c18158922de0cb3f60df6501b6d404563760

/**
 * 商品实体类
 */
public class Product implements Comparable<Product> {

    private String id;      // 商品ID
    private String name;    // 商品名称
    private double price;   // 价格
    private int stock;      // 库存
<<<<<<< HEAD
    private Set<String> tags; // 商品标签

    // 全参构造方法（包含标签）
    public Product(String id, String name, double price, int stock, Set<String> tags) {
=======

    public Product(String id, String name, double price, int stock) {
>>>>>>> a021c18158922de0cb3f60df6501b6d404563760
        this.id = id;
        this.name = name;
        this.price = price;
        this.stock = stock;
<<<<<<< HEAD
        // 若传入的标签为null，初始化为空的HashSet，否则使用传入的集合
        this.tags = (tags != null) ? new HashSet<>(tags) : new HashSet<>();
    }

    // 无标签的构造方法（方便不传入标签时使用）
    public Product(String id, String name, double price, int stock) {
        this(id, name, price, stock, null);
    }

    // Getters and Setters
=======
    }

    // Getters and Setters ...
>>>>>>> a021c18158922de0cb3f60df6501b6d404563760
    public String getId() { return id; }
    public void setId(String id) { this.id = id; }
    public String getName() { return name; }
    public void setName(String name) { this.name = name; }
    public double getPrice() { return price; }
    public void setPrice(double price) { this.price = price; }
    public int getStock() { return stock; }
    public void setStock(int stock) { this.stock = stock; }
<<<<<<< HEAD
    public Set<String> getTags() { return tags; }
    public void setTags(Set<String> tags) {
        // 确保标签集合不为null
        this.tags = (tags != null) ? new HashSet<>(tags) : new HashSet<>();
    }

    @Override
    public String toString() {
        // 基础信息部分
        StringBuilder sb = new StringBuilder(String.format(
                "ID: %-5s | 名称: %-20s | 价格: ¥%-8.2f | 库存: %d",
                id, name, price, stock
        ));
        // 标签部分（如果有标签则添加）
        if (!tags.isEmpty()) {
            sb.append(" | 标签：[").append(String.join("，", tags)).append("]");
        }
        return sb.toString();
=======

    @Override
    public String toString() {
        return String.format("ID: %-5s | 名称: %-20s | 价格: ¥%-8.2f | 库存: %d", id, name, price, stock);
>>>>>>> a021c18158922de0cb3f60df6501b6d404563760
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Product product = (Product) o;
        return Objects.equals(id, product.id);
    }

    @Override
    public int hashCode() {
        return Objects.hash(id);
    }

    @Override
    public int compareTo(Product other) {
        // 按商品ID的字母顺序升序排列
        return this.id.compareTo(other.id);
    }
}